package com.zhs.gccs.view;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhs.gccs.R;
import com.zhs.gccs.base.BaseActivity;
import com.zhs.gccs.model.TaskItem;
import com.zhs.gccs.adapter.MenuAdapter;
import com.zhs.gccs.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class TaskProjectList extends LinearLayout implements View.OnTouchListener{
    public TaskProjectList(Context context) {
        super(context);
        init(context);
    }

    public TaskProjectList(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public TaskProjectList(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private OnItemClickListener onItemClickListener;
    private List<TaskItem> taskItemList = new ArrayList<>();
    private MenuAdapter<TaskItem> adapter;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    private CommitClickListener commitClickListener;
    public void setCommitClickListener(CommitClickListener commitClickListener){
        this.commitClickListener = commitClickListener;
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }

    public interface CommitClickListener{
        void onCommitClickListener(String pid);
    }

    private TextView task_title;
    private TextView task_shuoming;
    private void init(Context context){
        View view = LayoutInflater.from(context).inflate(R.layout.item_task_list, null);
        addView(view);

        LinearLayout.LayoutParams params = (LayoutParams) view.getLayoutParams();
        params.width = (int) (BaseActivity.displayWidth * 0.41);
        params.height = BaseActivity.MATCH_PARENT;
        params.rightMargin = (int) (10 * BaseActivity.density);
        params.topMargin = (int) (10 * BaseActivity.density);
        params.bottomMargin = (int) (10 * BaseActivity.density);

        view.setLayoutParams(params);

        ImageView close = view.findViewById(R.id.item_task_list_close);
        close.setOnClickListener(view1 -> {
            closeListener.onClose(0);
//            setVisibility(GONE);
        });
        task_title = view.findViewById(R.id.task_title);

        task_shuoming = view.findViewById(R.id.task_shuoming);


        RecyclerView recyclerView = view.findViewById(R.id.item_task_list_rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
//        for (int i = 0; i < 3; i++) {
//
//            TaskItem taskItem1 = new TaskItem();
//            taskItem1.setTaskName("抽查齐河县xx项目工程措施项及非农化情况");
//            taskItem1.setTaskStatus(String.valueOf(i % 3));
//            taskItem1.setTaskTime("2020年7月9日~8月9日");
//            list.add(taskItem1);
//        }

        adapter = new MenuAdapter<TaskItem>(taskItemList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_task_list_item;
            }

            @Override
            public void convert(VH holder, TaskItem data, int position) {
                TextView item_task_list_item_tv1 = holder.getView(R.id.item_task_list_item_tv1);//任务名称

                ImageView img_ischeck = holder.getView(R.id.img_ischeck);

                item_task_list_item_tv1.setText(data.getTaskName());

                if(data.getIsCheck() == 1){
                    img_ischeck.setVisibility(VISIBLE);
                }else {
                    img_ischeck.setVisibility(GONE);
                }
            }
        };

        recyclerView.setAdapter(adapter);
        recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.bottom = (int) (BaseActivity.density * 10);
            }
        });
        adapter.setOnItemClickListener((position, item) -> {
            if(onItemClickListener != null){
                onItemClickListener.onItemClick(position, item);

            }
        });

    }

    public void setList(List<TaskItem> list){
        taskItemList.clear();
        taskItemList.addAll(list);
        adapter.notifyDataSetChanged();
    }

    public void setTaskTitle(String taskName){
        task_title.setText(taskName);
    }

    public void setTaskShuoming(String shuoming){
        task_shuoming.setText(shuoming);
    }

    private ItemPointDetail.CloseListener closeListener;
    public void setCloseListener(ItemPointDetail.CloseListener closeListener){
        this.closeListener = closeListener;
    }
    public interface CloseListener{
        void onClose(int state);
    }



}
